package com.zhoug.common.framework.glide.wrap;

import android.graphics.Bitmap;
import android.graphics.drawable.Drawable;

import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.zhoug.common.framework.glide.RoundedCorners;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.DrawableRes;
import jp.wasabeef.glide.transformations.GrayscaleTransformation;

/**
 * @Author 35574
 * @Date 2021/9/7
 * @Description
 */
class RequestOptionBuild {
    private boolean isCircle;//是否转化成圆形图片
    private boolean isGrayscale;//是否灰度转化
    private int roundedCorners;//圆角
    private boolean isCenterCrop=true;//ImageView是否设置的CenterCrop

    private RequestOptions mRequestOptions;

    public RequestOptionBuild() {
        mRequestOptions = new RequestOptions();
    }

    /**
     * 是否转化成圆形图片
     *
     * @param circle
     * @return
     */
    public RequestOptionBuild setCircle(boolean circle) {
        isCircle = circle;
        return this;
    }

    public boolean isCircle() {
        return isCircle;
    }

    /**
     * 是否灰度转化
     *
     * @param grayscale
     * @return
     */
    public RequestOptionBuild setGrayscale(boolean grayscale) {
        isGrayscale = grayscale;
        return this;
    }

    /**
     * 设置圆角
     * @param roundedCorners
     */
    public void setRoundedCorners(int roundedCorners) {
        this.roundedCorners = roundedCorners;
    }

    public int getRoundedCorners() {
        return roundedCorners;
    }

    public void setCenterCrop(boolean centerCrop) {
        isCenterCrop = centerCrop;
    }

    public boolean isCenterCrop() {
        return isCenterCrop;
    }

    /**
     * 设置占位图
     *
     * @param resourceId
     * @return
     */
    public RequestOptionBuild placeholder(@DrawableRes int resourceId) {
        mRequestOptions = mRequestOptions.placeholder(resourceId);
        return this;
    }

    /**
     * 设置占位图
     *
     * @param placeholder
     * @return
     */
    public RequestOptionBuild placeholder(Drawable placeholder) {
        mRequestOptions = mRequestOptions.placeholder(placeholder);
        return this;
    }

    /**
     * 设置加载图片失败后显示的错误图
     *
     * @param resourceId
     * @return
     */
    public RequestOptionBuild error(@DrawableRes int resourceId) {
        mRequestOptions = mRequestOptions.error(resourceId);
        return this;
    }

    /**
     * 设置加载图片失败后显示的错误图
     *
     * @param error
     * @return
     */
    public RequestOptionBuild error(Drawable error) {
        mRequestOptions = mRequestOptions.placeholder(error);
        return this;
    }



    @SuppressWarnings("all")
    public RequestOptions build() {
        List<Transformation<Bitmap>> transformations = new ArrayList<>();
        if (isCircle) {
            transformations.add(new CircleCrop());
        }
        if (isGrayscale) {
            transformations.add(new GrayscaleTransformation());
        }
        if (roundedCorners>0) {
            transformations.add(new RoundedCorners(roundedCorners,isCenterCrop));
        }
        if(transformations.size()>0){
            Transformation<Bitmap>[] trans=new Transformation[transformations.size()];
            for (int i = 0; i < transformations.size(); i++) {
                trans[i]=transformations.get(i);
            }
            transformations.clear();
            mRequestOptions = mRequestOptions.transform(trans);
        }
        return mRequestOptions;
    }


}
